<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends HYD_Controller {
    private $_edit_field_arr = array();
    private $_save_field_valid_arr = array(); //验证部分字段提交过来的值是否在有效范围内 
    
    public function __construct(){
        parent::__construct();
        $this->load->model('user/user_model');
        $this->field_lists_init();
    }
    
    private function field_lists_init(){  
        //修改action的数组
        $this->_edit_field_arr = array('id','name', 'sex');

        //生成规则
        $field_lists = array(); //text/dropdown/dropdownedit/checkbox/radio/date/time/datetime    
        $field_lists['id'] = array('rules' => 'required', 'ftype' => '', 'datasource' => '', 'default' => '', 'label' => '');
        $field_lists['name'] = array('rules' => '', 'ftype' => 'text', 'datasource' => '', 'default' => '', 'label' => '');
        $field_lists['sex'] = array('rules' => '','ftype' => 'checkbox', 'datasource' => array('F' => '女', 'M' => '男'), 'default' => '女', 'checked' => TRUE, 'label' => '');

        //设置label
        foreach ($field_lists as $field => $field_arr) {
            if (isset($field_arr['checked'])) {
                $this->_save_field_valid_arr[] = $field;
            }
        }
        $this->set_field_lists($field_lists);
    }

    public function edit_user_pinfo() {
        $field_arr = $this->_edit_field_arr;
        foreach ($this->get_field_lists() as $field => $rules) {
            if (in_array($field, $field_arr) && $rules['rules']) {
                $validation_data_para[$field] = ($this->input->post($field) != '' ? $this->input->post($field) : $rules['default']);
                if ($field == 'email' && !$this->input->post($field))
                    continue;
                $config_rules_para[] = array('field' => $field, 'label' => $rules['label'], 'rules' => 'trim|' . $rules['rules']);
            }
        }
        //$this->set_validation_data($validation_data_para);
        $this->set_validation_rules($config_rules_para);
        
        if ($this->do_validation() == FALSE) {
            $response = array('success' => FALSE, 'feedback' => validation_errors());
        } else {
            //对输入的密码进行规则检查
            $this->load->helper('hyd_password');
            if (0 && check_password_valid($this->input->post('password')) != 3) {
                $response = array('success' => FALSE, 'feedback' => '密码无效!');
            } else {
                $post_arr = array();
                foreach ($field_arr as $field) {
                    $post_value = $this->input->post($field);
                    $post_arr[$field] = $post_value;
                    //验证提交的值是否在有效范围内
                    if (in_array($field, $this->_save_field_valid_arr)) {
                        $field_valid_arr = $this->get_field_lists($field);
                        if (!isset($field_valid_arr['datasource'][$post_value])) {
                            $post_arr[$field] = $field_valid_arr['default'];
                        }
                    }
                }
                $response = $this->save_user($post_arr);
            }
        }
        
        $this->admin->do_userlog(get_class(), __FUNCTION__, $this->input->post('username'), $response['feedback'], $response['success'], FALSE);
        return $response;
    }

 
    /**
     * 取个人信息
     * @return boolean
     */
    public function load_user_pinfo() {
        $user_data = $this->session->userdata('user_data');
        $data = $this->user_model->get_data($user_data['id']);
        $user_data['name'] = $data['name'];
        $user_data['sex'] = $data['sex'];
        $response = array('success' => TRUE, 'data' => $user_data);
        return $response;
    }
    
    /**
     * 添加、修改时进行的保存
     * @return $response array
     */
    private function save_user($post_arr) {
        if (0) {
            return array('success' => FALSE, 'feedback' => '没有此操作权限');
        } else {
            $res = $this->user_model->save($post_arr['id'], $post_arr);
            switch ($res) {
                case 1:
                    $response = array('success' => TRUE, 'feedback' => '保存成功！');
                    break;
                case -1:
                    $response = array('success' => FALSE, 'feedback' => lang('ms_error_email_format'));
                    break;
                case -2:
                    $response = array('success' => FALSE, 'feedback' => lang('ms_error_email_already_exists'));
                    break;
                case MODEL_RETURN_DBERROR:
                    $response = array('success' => FALSE, 'feedback' => '数据库错误！');
                    break;
            }
            return $response;
        }
    }
    
}

/* End of file admin.php */
/* Location: ./application/controllers/admin.php */